﻿using System;
using System.Net;
using Platformer_Example.Controller.Data;
using Platformer_Example.Controller.Network;
using Platformer_Example.Controller.Network.Messages;

namespace Platformer_Example.Screen.QuickGame
{
    public partial class QuickGame
    {
        private NetworkServer _server;
        private ServerData _serverData;

        void StartServer(string playerName)
        {
            Console.WriteLine("Enter a game name");
            Console.Write(">> ");
            /*
            string gameName = Console.ReadLine();
            /*/
            string gameName = "My custom game #1";
            Console.WriteLine(gameName);
            //*/
            Console.WriteLine("Game name registered: {0}", gameName);

            var info = MapSummary.GetMapInfo();

            Console.WriteLine("Select which map you want to take:");
            for (int i = 0; i < info.Count; i++)
            {
                Console.WriteLine("({0}) {1}", i, info[i]);
            }
            Console.Write(">> ");
            /*
            int selectedMap = Convert.ToInt32(Console.ReadLine());
            /*/
            int selectedMap = 0;
            Console.WriteLine(selectedMap);
            //*/

            _server = new NetworkServer();
            _serverData = new ServerData();
            _server.OnSearchGame += OnServerSearchGame;
            _server.OnJoinGame += OnServerJoinGame;

            _serverData.GameName = gameName;
            _serverData.MapSummary = info[selectedMap];

            _serverData.LocalPlayer = _serverData.AddPlayer(playerName, null);
            _serverData.PrintClientTeamAssociation();

            /*
            Console.WriteLine("Press any key to start the game");
            Console.Write(">> ");
            Console.ReadKey();
            Console.WriteLine();
            //*/

            ServerStartGame();
        }

        void OnServerSearchGame(SearchGame message, IPAddress senderAddress)
        {
            _server.SendGameDescription(new SearchGameResponse
            {
                CurrentSlot = 1,
                Description = _serverData.GameName,
                MaxSlot = _serverData.MapSummary.TotalPlayers,
                MapName = _serverData.MapSummary.MapName,
            }, senderAddress);
        }

        void ServerStartGame()
        {
            _server.OnSearchGame -= OnServerSearchGame;
            _server.OnJoinGame -= OnServerJoinGame;

            var message = new Controller.Network.Messages.LoadGame
                              {
                                  MapName = _serverData.MapSummary.MapName,
                                  Teams = _serverData.Teams,
                                  Players = _serverData.GetPlayers(),
                              };

            _server.SendLoadGame(_serverData.ClientAddresses(), message);
            ChangeScreen(new LoadGame.LoadGame(_server, _serverData, message));
        }

        void OnServerJoinGame(JoinGame message, IPAddress senderAddress)
        {
            if (_serverData.HasAnySlotLeft())
            {
                var id = _serverData.AddPlayer(message.PlayerName, senderAddress);
                _server.SendJoinGameAcceptResponse(_serverData.ClientAddresses(), new JoinGameAcceptResponse
                                                                      {
                                                                          MapName = _serverData.MapSummary.MapName,
                                                                          NetworkId = id.ToString(),
                                                                          Teams = _serverData.GetTeamNamesList(),
                                                                          PlayersNames = _serverData.GetPlayerNamesList(),
                                                                      });
                _serverData.PrintClientTeamAssociation();
            }
            else
            {
                _server.SendJoinGameDeclineResponse(senderAddress);
            }
        }
    }
}